package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.InputStream;
import java.util.*;


@Controller
@RequestMapping("/cargo/finance")
@Slf4j
public class FinanceController extends BaseController{
    @Reference
    private FinanceService financeService;

    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;

    @Reference
    private InvoiceService invoiceService;


    //1.生成委托单
    @RequestMapping("/add")
    public String shippingAdd(String id){
        Invoice invoice = invoiceService.findById(id);
        PackingList packingList = packingService.findBypackingId(id);
        String exportIds = packingList.getExportIds();
        String[] exportIdArr = exportIds.split(",");
        Export export = null;
        if (exportIdArr != null && exportIdArr.length > 0) {
            export = exportService.findById(exportIdArr[0]);
        }
        if (invoice.getStatus() == 1) {
            Finance finance = new Finance();
            finance.setFinanceId(invoice.getId());
            finance.setInvoiceMoney(invoice.getInvoiceMoney());
            finance.setInvoiceTime(invoice.getInvoiceTime());
            finance.setInvouceId(invoice.getInvoiceNo());
            finance.setExportNos(packingList.getExportNos());
            finance.setShipmentPort(export.getShipmentPort());
            finance.setDestinationPort(export.getDestinationPort());
            finance.setConsignee(export.getConsignee());
            request.setAttribute("finance",finance);
            return "/cargo/finance/finance-add";
        }
        return "redirect:/cargo/invoice/list";
    }

    @RequestMapping("/list")
    public String contractList(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize) {
        log.info("执行查询委托单列表:");
        FinanceExample example = new FinanceExample();
        PageInfo<Finance> pageInfo = financeService.findByPage(example, pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "cargo/finance/finance-list";
    }

    @RequestMapping("/edit")
    public String edit(Finance finance){
        financeService.save(finance);
        return "redirect:/cargo/invoice/list";
    }

    @RequestMapping("/financePdf")
    public void financePdf(String id) throws Exception {
        InputStream in = session.getServletContext().getResourceAsStream("/jasper/finance.jasper");
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        Finance finance = financeService.selectByPrimaryKey(id);

        map.put("inputDate",new Date());
        map.put("invouceId",finance.getInvouceId());
        map.put("invoiceMoney",finance.getInvoiceMoney());
        map.put("invoiceTime",finance.getInvoiceTime());
        map.put("shipmentPort",finance.getShipmentPort());
        map.put("exportNos",finance.getExportNos());
        map.put("destinationPort",finance.getDestinationPort());
        map.put("consignee",finance.getConsignee());

        list.add(map);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-disposition","attachment;fileName=finance.pdf");
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
        JasperPrint jasperPrint = JasperFillManager.fillReport(in, null, dataSource);
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
        in.close();
    }

    @RequestMapping("checkState")
    @ResponseBody
    public Map<String,Integer> checkState(String id){
        // 返回的数据通过map封装
        Map<String,Integer> resultMap = new HashMap<>();
        Invoice invoice = invoiceService.findById(id);
        if (invoice.getStatus()==1){
            resultMap.put("message",1);
        } else {
            resultMap.put("message",0);
        }
        return resultMap;
    }
}
